package
{
	import GAM.*;
	
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
[SWF(width="500",height="400")]
	public class GAM extends Sprite
	{
		private var Stage:stage1;
		private var timer:Timer=new Timer(200);
		private var itemtimer:Timer=new Timer(6000);
		private var lasttimer:Timer=new Timer(7500);
		private var tmptimer:Timer=new Timer(200,1);
		private var eat:Boolean=false;
		private var Snake:snake;
		private var a:Sprite;
		private var Item:item;
		public function GAM()
		{
			init();
				
		}
		public function init():void 
		{
			Stage=new stage1();
			Snake=new snake();
			
			addChild(Stage);
			Stage.addChild(Snake);
			timer.addEventListener("timer",ontimer);
			timer.start();
			//itemtimer.addEventListener(TimerEvent.TIMER,onitemtimer);
			
			
			itemtimer.start();
			stage.focus=Snake;
			Snake.addEventListener(KeyboardEvent.KEY_DOWN,onkeydown);
			CreateItem();
			lasttimer.addEventListener(TimerEvent.TIMER,onlasttimer);
			lasttimer.start();
			addChild(Item);
			
			
		}
		public function ontimer(event:TimerEvent):void
		{
			Snake.move();
			
			if(Snake.Snakehead.x==Item.x&&Snake.Snakehead.y==Item.y)
			{
				Snake.addChildNode();
				eat=true;
				removeChild(Item);
				//lasttimer.removeEventListener(TimerEvent.TIMER,onlasttimer);
				lasttimer.stop();
				CreateItem();
				addChild(Item);
				//lasttimer.addEventListener(TimerEvent.TIMER,onlasttimer);
				lasttimer.start();
					
				
			}
			else
			{eat=false;}
			if(Comflict()||Snake.complete())
			{
				timer.removeEventListener("timer",ontimer);
			//	itemtimer.removeEventListener(TimerEvent.TIMER,onitemtimer);
				
			}
			
		}
		/*public function onitemtimer(event:TimerEvent):void
		{
			CreateItem();
			addChild(Item);
			
		}
		*/
		public function onkeydown(event:KeyboardEvent):void
		{
			var olddirection:String;
			olddirection=Snake.direction;
			
			
			switch(event.keyCode.toString())
			{
				case "38":
					Snake.direction="N";
					if(olddirection=="S")
						Snake.direction=olddirection;
					break;
				case "40":
					Snake.direction="S";
					if(olddirection=="N")
						Snake.direction=olddirection;
					break;
				case "39":
					Snake.direction="E";
					if(olddirection=="W")
						Snake.direction=olddirection;
					break;
				case "37":
					Snake.direction="W";
					if(olddirection=="E")
						Snake.direction=olddirection;
					break;
				default:
					break;
			}
		}
		public function onlasttimer(event:TimerEvent):void
		{
			
			removeChild(Item);
			if(!eat)
			{
				CreateItem();
				addChild(Item);
				lasttimer.addEventListener(TimerEvent.TIMER,onlasttimer);
				lasttimer.start();
			}
			
			
			
		}
		public function CreateItem():void
		{
			while(1)
			{
			var x:int=Math.random()*19;
			var y:int=Math.random()*15;
			var px:int=25*x;
			var py:int=25*y;
			if((!Snake.repeat(px,py))&&(!(px==Snake.Snakehead.x&&py==Snake.Snakehead.y)))
			{
				Item=new item();
				Item.x=px;
				Item.y=py;
				
				break;
			}
			}
		}
		public function Comflict():Boolean
		{
			if(Snake.repeat(Snake.Snakehead.x,Snake.Snakehead.y)||(Snake.Snakehead.x<0)||(Snake.Snakehead.x>475)||(Snake.Snakehead.y<0)||(Snake.Snakehead.y>375))
			{
				return true;
			}
			else return false;
		}
		/*public function ontmptimer(event:TimerEvent):void
		{
			CreateItem();
			addChild(Item);
		}
		*/
		
	}
}